package com.qf.fmall2202.shopcart.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.qf.fmall2202.product.entity.Product;
import com.qf.fmall2202.product.entity.ProductImg;
import com.qf.fmall2202.product.entity.ProductSku;
import com.qf.fmall2202.product.service.IProductImgService;
import com.qf.fmall2202.product.service.IProductService;
import com.qf.fmall2202.product.service.IProductSkuService;
import com.qf.fmall2202.shopcart.entity.ShoppingCart;
import com.qf.fmall2202.shopcart.service.IShoppingCartService;
import com.qf.fmall2202.utils.R;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 购物车  前端控制器
 * </p>
 *
 * @author dong_
 * @since 2022-11-08
 */
@CrossOrigin
@RestController
@RequestMapping("/shopcart")
public class ShoppingCartController {

    @Autowired
    IShoppingCartService shoppingCartService;

    @GetMapping("/listbycids")
    public R listbycids(String cids){

        List<ShoppingCart> shoppingCarts = shoppingCartService.listbycids(cids);
        return R.ok().put("data", shoppingCarts);

    }


    @PutMapping("/update/{cid}/{num}")
    public R update(@PathVariable("cid") Integer cid,@PathVariable("num") String num){

        final UpdateWrapper<ShoppingCart> shoppingCartUpdateWrapper = new UpdateWrapper<>();
        shoppingCartUpdateWrapper.set("cart_num",num);
        shoppingCartUpdateWrapper.eq("cart_id",cid);

        shoppingCartService.update(shoppingCartUpdateWrapper);

        return R.ok();

    }


    @GetMapping("/list")
    public R list(Integer userId){

        List<ShoppingCart> shopcartList = shoppingCartService.listByUserId(userId);
        return R.ok().put("data", shopcartList);

    }


    @PostMapping("/add")
    public R add(@RequestBody ShoppingCart shoppingCart){

        // 设置保存时间
        final Date date = new Date();

        final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        final String dateStr = simpleDateFormat.format(date);

        shoppingCart.setCartTime(dateStr);
        shoppingCartService.save(shoppingCart);

        return R.ok();
    }

}
